package com.zzh.myapplication.popuwindpw;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import net.icarplus.car.R;
import net.icarplus.car.datedialog.DateTimePicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2016/3/29.
 */
public class ShowPopuWindow {
    public DateTimePicker mDateTimePicker;
    private String timeString = "";
    private PopupWindow popupWindow;
    private String nowTimeString = "";//校验用，当前时间
    private long time;//时间选择器回调
    private Calendar mDate = Calendar.getInstance();
    private onSelectTimeString onSelectTimeString;

    public void showTimePop(Context context,View vi, final int what, final onSelectTimeString onSelectTimeString) {
        View popView = View.inflate(context, R.layout.popup_window_time_wheel2, null);
        mDateTimePicker = (DateTimePicker) popView.findViewById(R.id.date);//选择器2
        Button qd = (Button) popView.findViewById(R.id.qd);//提交确定键
        this.onSelectTimeString = onSelectTimeString;
        qd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (what == 0) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.MINUTE, -1);//为增加用户体验选择器一分钟以前的时间
                    calendar.set(Calendar.SECOND, 0);
                    Date date = calendar.getTime();
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    nowTimeString = formatter.format(date);
                    if (onSelectTimeString != null) {
                        onSelectTimeString.onTimeString(timeString);
                    }
                    /*if (!timeString.equals("")) {
                        takeCarTimeTxt.setText("" + timeString);
                    } else {

                    }*/
                }
                popupWindow.dismiss();
            }
        });
        View rl2 = popView.findViewById(R.id.rl2);
        View rl3 = popView.findViewById(R.id.rl3);
        rl2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
            }
        });
        rl3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
            }
        });
        mDateTimePicker.setVisibility(View.VISIBLE);
        //----------第二种时间选择器
        mDateTimePicker.setOnDateTimeChangedListener(new DateTimePicker.OnDateTimeChangedListener() {

            @Override
            public void onDateTimeChanged(DateTimePicker view, int year, int month,
                                          int day, int hour, int minute) {
                // TODO Auto-generated method stub
                mDate.set(Calendar.YEAR, year);
                mDate.set(Calendar.MONTH, month);
                mDate.set(Calendar.DAY_OF_MONTH, day);
                mDate.set(Calendar.HOUR_OF_DAY, hour);
                mDate.set(Calendar.MINUTE, minute);
                mDate.set(Calendar.SECOND, 0);
                updateText(mDate.getTimeInMillis(), year, month + 1, day, hour, minute);//时间
            }
        });
        mDate = Calendar.getInstance();
//		if(what == 0){
//			mDate.add(Calendar.HOUR_OF_DAY, 2);
//			timeString = takeCarTimeTxt.getText().toString();
//		}else{
//			mDate.add(Calendar.HOUR_OF_DAY, 4);
//			timeString = backCarTimeTxt.getText().toString();
//		}
        time = mDate.getTimeInMillis();//第二种的回调时间
        Date date = mDate.getTime();
        mDateTimePicker.setDate(date);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        timeString = formatter.format(date);
        popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        // 需要设置一下此参数，点击外边可消失
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击窗口外边窗口消失
        popupWindow.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(vi, Gravity.CENTER, 0, 0);
    }

    private void updateText(long date, int year, int month, int day, int hour, int minute) {//时间选择器的回调
        time = date;
        String monthString = "";
        String dayString = "";
        String hourString = "";
        String minuteString = "";
        if ((month) < 10) {
            monthString = "0" + (month);
        } else {
            monthString = "" + (month);
        }
        if (day < 10) {
            dayString = "0" + day;
        } else {
            dayString = "" + day;
        }
        if (hour < 10) {
            hourString = "0" + hour;
        } else {
            hourString = "" + hour;
        }
        if (minute < 10) {
            minuteString = "0" + minute;
        } else {
            minuteString = "" + minute;
        }

        timeString = "" + year + "-" + monthString + "-" + dayString + " " + "" + hourString + ":" + minuteString;
    }

    public interface onSelectTimeString {
       public void onTimeString(String timeString);
    }

}
